import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect: '/login'  //登录页导航重定向
  },
  {
    path:'/login',
    component:Login
  },
  {
    path:'/home',
    component: () => import('@/views/Home.vue'),
    redirect: '/welcome',  //二级重定向，只要访问home都重定向到welcome
    children:[
      {
        path: '/welcome',
        component: () => import('@/components/Welcome.vue')
      },
      {
        path: '/users',
        component: () => import('@/components/Users.vue')
      },
      {
        path: '/rights',
        component: () => import('@/components/PowerList.vue')
      },
      {
        path: '/roles',
        component: () => import('@/components/Roles.vue')
      },
      {
        path: '/goods',
        component: () => import('@/components/Goods.vue')
      },
      {
        path: '/categories',
        component: () => import('@/components/Categories.vue')
      },
      {
        path: '/params',
        component: () => import('@/components/Params.vue')
      },
      {
        path: '/goods/addgoods',
        component: () => import('@/components/Addgoods.vue')
      },
      {
        path: '/orders',
        component: () => import('@/components/Orders.vue')
      },
    ]
  }

]

const router = new VueRouter({
  routes
})

//导航守卫
router.beforeEach((to, from, next) => {
  //用户去往登录页面直接放行
  if(to.path === '/login'){
    return next();
  }
  //用户不是去往登录页面就要看用户是否登录
  var userInfo = JSON.parse(sessionStorage.getItem('userInfo'));
  if(!userInfo){
    return next('/login');
  }
  //用户登录了也放行
  next();
})


export default router
